/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BarCode;

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;



/**
 *
 * @author Juan
 */
public final class ImprimirCode {
    
    /*
     * 
     * segun un tutorial que encontre esto deberia funcionar... pero toca probarlo...
     * si alguno tiene impresora y puede bien.. sino tratare de probarlo  lo mas pronto posible...
     * */
    
    public ImprimirCode(String cod) throws Exception{
        imprimir(cod);
    }
    
    
    public void imprimir(String cod) throws Exception{
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));
    pras.add(null);
    PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, pras);
    if (pss.length == 0) {
            throw new RuntimeException("No se detecta impresora disponible.");
        }
    PrintService ps = pss[0];
    System.out.println("Imprimiendo en " + ps);
    DocPrintJob job = ps.createPrintJob();
    FileInputStream fin = new FileInputStream("src/CodigosGenerados/"+cod+".png");
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.PNG, null);
    job.print(doc, pras);
    /*///////////////////////////////////////////////////////-----------------------
    PrintRequestAttributeSet prass = new HashPrintRequestAttributeSet();
    prass.add(new Copies(1));
    PrintService[] pss2=PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, pras);
    PrintService ps2 = pss2[0];
    if (pss2.length == 0) {
            throw new RuntimeException("No se detecta impresora disponible.");
        }
    String direccion="src/CodigosGenerados/Abc12345.png";
    Imprimible i= new Imprimible();
    
        
    PrinterJob jobb = (PrinterJob) ps2.createPrintJob();//PrinterJob.getPrinterJob();
    jobb.setPrintable( i);
    FileInputStream fin2 = new FileInputStream("src/CodigosGenerados/"+cod+".png");
    Doc doc2= new SimpleDoc(fin2, DocFlavor.INPUT_STREAM.PNG, null);
    DocPrintJob job3= (DocPrintJob) jobb;
    job3.print(doc2,pras);
    jobb.print();
///*
try 
{
   jobb.print();
} 
catch (PrinterException e) 
{
   e.printStackTrace();
}*/
//////////////////////////////////////////////////////////-------------------------
    fin.close();
  
    
}
}
